<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item label="银行网点联行号"
                           :labelCol="{span: 7}"
                           :wrapperCol="{span: 16, offset: 1}">
                <a-input placeholder="请输入银行网点联行号" v-model="queryParams.bankBranchCode"/>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item label="银行网点名称"
                           :labelCol="{span: 7}"
                           :wrapperCol="{span: 16, offset: 1}">
                <a-input placeholder="请输入银行网点名称" v-model="queryParams.bankBranchName"/>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item label="银行名称"
                           :labelCol="{span: 7}"
                           :wrapperCol="{span: 16, offset: 1}">
                <a-input placeholder="请输入银行名称" v-model="queryParams.bankName"/>
              </a-form-item>
            </a-col>
          </a-row>

        </div>
        <div style="text-align: center" >
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </div>
      </a-form>
    </div>

    <div>
      <div class="operator">
        <a-button v-hasPermission="['bankCode:add']" ghost type="primary" @click="addBankCode">新增</a-button>
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.id"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 1186 }"
               @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="bankCodeOperation" slot-scope="text, record">
          <a-icon v-hasPermission="['bankCode:edit']" type="setting" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="editBankCode(record)" title="修改"></a-icon>&nbsp;
          <a-icon v-hasPermission="['bankCode:cancel']" type="poweroff" twoToneColor="#4a9ff5"
                  @click="cancelBankCode(record)" title="注销"></a-icon>&nbsp;
          <a-icon v-hasPermission="['bankCode:view']" type="eye" theme="twoTone" twoToneColor="#42b983"
                  @click="viewBankCode(record)" title="查看"></a-icon>
          <a-badge v-hasNoPermission="['bankCode:edit','bankCode:cancel','bankCode:view']"
                   userStatus="warning" text="无权限"></a-badge>
        </template>

      </a-table>

      <!-- 操作 -->
      <bankCode-operation
        ref="bankCodeOperation"
        @close="handleBankCodeOperationClose"
        @success="handleBankCodeOperationSuccess"
        :bankCodeOperationVisiable="bankCodeOperation.visiable">
      </bankCode-operation>

    </div>
  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate';
  import {mapState} from "vuex"
  import BankCodeOperation from "./BankCodeOperation";

  export default {
    name: "BankCode",
    components: {BankCodeOperation, RangeDate},
    data() {
      return {
        advanced: false,
        bankCodeOperation: {
          visiable: false,
        },

        queryParams: {
          bankBranchCode: '',
          bankBranchName: '',
          bankName: ''
        },

        dataSource: [],
        sortedInfo: null,
        paginationInfo: null,
        selectedRowKeys: [],
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false,

      }
    },
    computed: {
      ...mapState({
        user: state => state.account.user,
      }),
      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        let {filteredInfo} = this
        filteredInfo = filteredInfo || {}
        return [{
          title: '银行网点联行号',
          dataIndex: 'bankBranchCode',
          scopedSlots: {customRender: 'bankBranchCode'},
        }, {
          title: '银行网点名称',
          dataIndex: 'bankBranchName',
          scopedSlots: {customRender: 'bankBranchName'},
        }, {
          title: '银行名称',
          dataIndex: 'bankName',
          scopedSlots: {customRender: 'bankName'},
        }, {
          title: '行政区划代码',
          dataIndex: 'regionCode',
          scopedSlots: {customRender: 'regionCode'},
        }, {
          title: '网联机构号',
          dataIndex: 'nuccInstId',
          scopedSlots: {customRender: 'nuccInstId'},
        }, {
          title: '银行网点名称',
          dataIndex: 'cupsInstId',
          scopedSlots: {customRender: 'cupsInstId'},
        }, {
          title: '城商银行清算中心机构号',
          dataIndex: 'ccfccbInstId',
          width: 200,
          scopedSlots: {customRender: 'ccfccbInstId'},
        }, {
          title: '状态',
          width: 60,
          dataIndex: 'bankBranchStatus',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return '非正式'
              case '01':
                return '正式'
              case '02':
                return '停用'
              case '03':
                return '已注销'
              default:
                return text
            }
          },
        }, {
          title: '操作',
          dataIndex: 'bankCodeOperation',
          fixed: 'right',
          width: 100,
          show: false,
          scopedSlots: {customRender: 'bankCodeOperation'}
        }]
      }
    },
    mounted() {
      this.fetch()
    },
    methods: {

      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },

      handleBankCodeOperationClose() {
        this.bankCodeOperation.visiable = false
      },
      handleBankCodeOperationSuccess() {
        this.bankCodeOperation.visiable = false
        this.$message.success('操作成功')
        this.search()
      },


      addBankCode() {
        this.bankCodeOperation.visiable = true
      },
      viewBankCode(record) {
        record.operation = 'view'
        this.$refs.bankCodeOperation.setBankCodeFormValues(record)
        this.bankCodeOperation.visiable = true
      },
      editBankCode(record) {
        record.operation = 'edit'

        if (record.bankBranchStatus == "03") {
          this.$message.warning("已注销数据不允许修改");
        } else {
          this.$refs.bankCodeOperation.setBankCodeFormValues(record)
          this.bankCodeOperation.visiable = true
        }
      },
      cancelBankCode(record) {
        record.operation = 'cancel'
        if (record.bankBranchStatus == "03") {
          this.$message.warning("已注销数据不允许重复注销");
        } else {
          this.$refs.bankCodeOperation.setBankCodeFormValues(record)
          this.bankCodeOperation.visiable = true
        }
      },

      search() {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        let params = this.queryParams

        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...params,
          ...filteredInfo
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // // 清空时间选择
        // this.$refs.createTime.reset()
        this.fetch()
      },
      handleTableChange(pagination, filters, sorter) {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        // 将这三个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        this.filteredInfo = filters
        this.sortedInfo = sorter

        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },
      fetch(params = {}) {
        this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('/admin/merParam/bankCodeInfo', {
          ...params
        }).then((r) => {
          let data = r.data
          const pagination = {...this.pagination}
          pagination.total = data.total
          this.dataSource = data.rows
          this.pagination = pagination
          this.loading = false
        })
      },
    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../../../static/less/Common";
</style>

